package com.xci.platform.core;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;

import java.io.Serializable;
import java.util.List;

/**
 * 封装布尔消息，消息中封装两个字段：一个表示操作是否成功，一个表示操作消息
 *
 * @param <T> 相关对象类型
 */
public class BoolMessage<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /// <summary>
    /// 成功消息，消息内容为空
    /// </summary>
    public static final BoolMessage<Object> True = new BoolMessage<>(true);

    /// <summary>
    /// 失败消息，消息内容为空
    /// </summary>
    public static final BoolMessage<Object> False = new BoolMessage<>(false);

    /**
     * 成功状态
     */
    private boolean success;

    /**
     * 状态消息
     */
    private String msg;

    /**
     * 数据对象
     */
    private T data;

    /**
     * 从指定的布尔状态来初始化布尔消息
     *
     * @param success 状态
     */
    public BoolMessage(boolean success) {
        this.success = success;
    }

    /**
     * 从指定的布尔状态和状态消息来初始化布尔消息
     *
     * @param success 状态
     * @param msg     消息
     */
    public BoolMessage(boolean success, String msg) {
        this.success = success;
        this.msg = msg;
    }

    /**
     * 从指定的布尔状态和状态消息来初始化布尔消息
     *
     * @param success 状态
     * @param data    数据对象
     */
    public BoolMessage(boolean success, T data) {
        this.success = success;
        this.data = data;
    }

    /**
     * 从指定的布尔状态和状态消息来初始化布尔消息
     *
     * @param success 状态
     * @param msg     v
     * @param data    数据对象
     */
    public BoolMessage(boolean success, String msg, T data) {
        this.success = success;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 从指定的布尔状态和状态消息来初始化布尔消息
     *
     * @param data 数据对象
     */
    public BoolMessage(T data) {
        this.success = true;
        this.data = data;
    }

    /**
     * 合并消息
     *
     * @param msgs 多个消息
     * @return 返回合并后的消息对象
     */
    public static BoolMessage merge(BoolMessage... msgs) {
        if (msgs == null || msgs.length == 0) {
            return BoolMessage.True;
        }

        boolean success = true;
        StringBuilder message = new StringBuilder();
        for (BoolMessage current : msgs) {
            success = success && current.isSuccess();
            if (current.getMsg() != null && !current.getMsg().equals("")) {
                message.append(current.getMsg()).append(";");
            }
        }
        return new BoolMessage(success, StrUtil.removeSuffix(message.toString(), ";"));
    }

    public static <T> BoolMessage<T> success(T data) {
        return new BoolMessage<T>(true, data);
    }

    public static BoolMessage success(String msg) {
        return new BoolMessage(true, msg);
    }

    public static <T> BoolMessage<T> success(String msg, T data) {
        return new BoolMessage<>(true, msg, data);
    }

    public static BoolMessage fail(String msg) {
        return new BoolMessage(false, msg);
    }

    public static <T> BoolMessage<T> fail(String msg, T data) {
        return new BoolMessage<>(false, msg, data);
    }

    public static JsonGrid pagegrid(List list) {
        return new JsonGrid((Page) list);
    }

    public static JsonGrid grid(Page pageList) {
        return new JsonGrid(pageList);
    }

    public static JsonGrid grid(List list) {
        return new JsonGrid(list);
    }

    public static JsonGrid grid(List list, Long total) {
        return new JsonGrid(list, total);
    }

    public static BoolMessage tree(List list) {
        return new BoolMessage<>(list);
    }

    public boolean isSuccess() {
        return success;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

    @Override
    public String toString() {
        return "JsonResult{" +
                "success=" + success +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}
